package game;

import javax.microedition.lcdui.Display;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;

import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGElement;

/**
 * Listener for the highscore-screen
 * @author Gustav
 * @since 29.5.2010
 */
class MIDPSVGEventListenerHighscore implements SVGEventListener {
    private SVGAnimator svgAnimator;
    private Document doc;
    int x = 0, y = 0;

    /**
     * One-time initialization (load the data from the database)
     */
    public MIDPSVGEventListenerHighscore(SVGAnimator svgAnimator, SVGImage svgImage) {
        this.svgAnimator = svgAnimator;
        this.doc = svgImage.getDocument();
    }

    public void init(){
        SVGElement text;
        String[] v = new String[100];
        LUMM2_Midlet.rms.printScores().copyInto(v);
        if(v.length == 0)
                v[0] = "no entries";
        for(int i=1;i<=Math.min(v.length,10);i++){
                text = (SVGElement)doc.getElementById(i+"-2");
                text.setTrait("#text", v[i-1].substring(0, v[i-1].indexOf(" ")));
                text = (SVGElement)doc.getElementById(i+"-3");
                text.setTrait("#text", v[i-1].substring(v[i-1].indexOf(" "),v[i-1].length()));
        }
    }

    /**
     * user pressed enter: go back to the menu
     */
    public void keyPressed(int keystate) {
    	if(keystate == -5) { //enter, stops the menu and change canvas
                svgAnimator.stop();
    		LUMM2_Game.currentGameState = LUMM2_Game.MENU;
    		Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(LUMM2_Midlet.pc);
                System.out.println("1");
    	}
    }

    /**
     * other methos I dont need... but had to implement
     */
    public void keyReleased(int k) { /* ... */ }
    public void hideNotify() { /* ... */ }
    public void pointerPressed(int a, int b) { /* ... */ }
    public void pointerReleased(int a, int b) { /* ... */ }
    public void showNotify() { /* ... */ }
    public void sizeChanged(int a, int b) { /* ... */ }
}
